/*
 * @Description: User持久层的接口
 * @Author: ljl
 * @Date: 2024-11-25 15:11:38
 * @LastEditors: rendc
 * @LastEditTime: 2024-11-26 10:25:54
 */
package com.briup.demo.mapper;

import com.briup.demo.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;

// 在程序运行时 框架会自动生成接口的实现类对象 并交给spring的IOC容器管理
@Mapper //表示这个是一个mybatis的Mapper接口
public interface UserMapper {
    public void deleteByIdXML(Integer id);

    // 根据id删除用户
    // 参数 id 用户id
    // 返回值 无
    @Delete("delete from es_user where id = #{id}")
    public void deleteUserById(Integer id);

//     根据id查询用户信息
    public int getUserById(Integer id);

    // 多条件查询用户信息
    public List<User> getUserByCondition(
            @Param("name") String name,
            @Param("status") Byte status,
            @Param("begin") LocalDateTime begin,
            @Param("end") LocalDateTime end
    );
    public List<User> getUserByConditionNew(
            @Param("name") String name,
            @Param("status") Byte status,
            @Param("begin") LocalDateTime begin,
            @Param("end") LocalDateTime end
    );
//    public void updateUser(User user);

    public void delUserByIds(@Param("ids") List<Integer> ids);








    // 写sql语句 - 查询所有用户数据
    // @Select代表写的是select语句
    @Select("select * from t_user")
    public List<User> allUserList();

    // 根据id查询用户
    //手动映射结果
    @Results({@Result(column ="register_time",property = "registerTime")})
    @Select("select id,name,age,gender,phone,birthday,register_time from t_user where id = #{id}")
    public User getById(Integer id);

    // 组合查询
    // 姓张 女 注册时间2024-11-25 10:45:50之后的  2024-11-26 10:45:50之前的 降序
    @Select("select * from t_user " +
            "where name like '%${name}%' " +
            "and gender = ${gender} " +
            "and register_time between #{beginTime} and #{endTime} " +
            "order by register_time desc "
    )
    public List<User> querySelect
    (@Param("name") String name,@Param("gender") short gender, @Param("beginTime") LocalDateTime beginTime,@Param("endTime") LocalDateTime endTime);
    //  @Select("SELECT * FROM t_user " +
    //          "WHERE name LIKE CONCAT('%', #{name}, '%') " +
    //          "AND gender = #{gender} " +
    //          "AND register_time BETWEEN #{beginTime} AND #{endTime} " +
    //          "ORDER BY register_time DESC"
    //  )
    //  List<User> getUserList(
    //          @Param("name") String name,
    //          @Param("gender") Short gender,
    //          @Param("beginTime") LocalDateTime beginTime,
    //          @Param("endTime") LocalDateTime endTime
    //  );

    // 根据id删除用户
    @Delete("delete from t_user where id = #{id}")
    public void deleteById(Integer id);    
    
    // 登录
    // 无法防止sql注入
    // @Select("select * from t_user where username=${username} and password=${password} ")
    // 可以预防sql注入
    @Select("select * from t_user where id=${id} and phone=${phone} ")
    public List<User> login(@Param("id") String id, @Param("phone") String phone);
    
    // 新增 - 注册
    // 要求返回主键 赋值给user对象的id属性上
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into t_user(name,age,gender,phone,birthday,register_time) values(#{name},#{age},#{gender},#{phone},#{birthday},#{registerTime})")
    public void addUser(User user);

    // 编辑
    @Update("update t_user set name = #{name},age = #{age},gender = #{gender},phone = #{phone},birthday = #{birthday},register_time = #{registerTime} where id = #{id}")
    public void updateUser(User user);

}
